<template>
	<view class="box">
    <view class="address-choose-box" v-if="JSON.stringify(address) === '{}'">
        <button type="primary" size="mini" class="btnChooseAddress" @click="getAddress">请选择收货地址+</button>
      </view>
      <view class="address-info-box" @click="getAddress" v-else>
          <view class="row1">
            <view class="row1-left">
              <view class="username">收货人：<text>{{address.userName}}</text></view>
            </view>
            <view class="row1-right">
              <view class="phone">电话：<text>{{address.telNumber}}</text></view>
              <uni-icons type="arrowright" size="16"></uni-icons>
            </view>
          </view>
          <view class="row2">
            <view class="row2-left">收货地址：</view>
            <view class="row2-right">{{detail}} </view>
          </view>
        </view> 
		<image src="../../static/cart_border@2x.png" mode="" class="border_image"></image>
	</view>
</template>

<script>
  import {mapState,mapMutations,mapGetters} from 'vuex'
	export default {
		data() {
			return {
       
			};
		},
    methods:{
      ...mapMutations('m_address',["saveAddress"]),
      async getAddress(){
        // catch捕获错误
        const [err,succ]=await uni.chooseAddress().catch(err=>err)
        console.log(succ);
        if(succ.errMsg==="chooseAddress:ok"){
          this.saveAddress(succ)
        }
        
        // 判断授权与否
        if (err && (err.errMsg === 'chooseAddress:fail auth deny' || err.errMsg === 'chooseAddress:fail authorize no response')) {
            this.reAuth() // 调用 this.reAuth() 方法，向用户重新发起授权申请
          }
      },
      reAuth(){
        const [err2,res]=uni.showModal({
          content:"检测到您没打开地址权限，是否去设置打开？",
          confirmText:"确认",
          cancelText:"取消",
        })
        if(res.cancel){
          uni.$showMsg("您取消了地址的授权")
        }
        if(res.confirm){
          uni.openSetting({
            success:(res)=>{
              if (res.authSetting['scope.address']) return uni.$showMsg('授权成功！请选择地址')
                    // 3.4.3 地址授权的值等于 false，提示用户 “您取消了地址授权”
                    if (!res.authSetting['scope.address']) return uni.$showMsg('您取消了地址授权！')
            }
          })
        }
      }
    },
    computed:{
      ...mapState('m_address',["address"]),
      ...mapGetters('m_address',["detail"])
    }
	}
</script>

<style lang="scss">
  .border_image{
    display: block;
    width: 100%;
    height: 5px;
  }
  .address-choose-box {
    height: 90px;
    display: flex;
    align-items: center; 
    justify-content: center;
  }
  .address-info-box{
    padding: 20px 0;
    display: flex;
    flex-direction: column;
  }
  .row1{
    display: flex;
    justify-content: space-between;
    .row1-right{
      display: flex;
      align-items: center;
    }
    .phone{
        margin-right: 5px;
    }
   }
   .row2{
     display: flex;
     margin-top: 10px;
     align-items: center;
     .row2-left{
           white-space: nowrap;
     }

   }   
</style>
